package top.jolyoulu.core.framework.service

import top.jolyoulu.core.framework.common.TService
import top.jolyoulu.core.framework.dao.WordCountDao

/**
 * @Author: JolyouLu
 * @Date: 2024/5/15 17:58
 * @Description
 */
class WordCountService extends TService{

  private val wordCountDao = new WordCountDao()

  def dataAnalysis(): Array[(String,Int)] = {

    val path: String = this.getClass.getClassLoader.getResource("datas/2.txt").toURI.getPath
    val words = wordCountDao.readFile(path)

    val wordToOne = words.map(word => (word, 1))

    val wordGroup = wordToOne.groupBy(
      t => t._1
    )

    val wordToCount = wordGroup.map {
      case (word, list) => {
        list.reduce((t1, t2) => {
          (t1._1, t1._2 + t2._2)
        })
      }
    }

    wordToCount.collect()
  }
}
